{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "import numpy as np\n",
    "import pandas as pd\n",
    "import gzip"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "source": [
    "dx_icd = pd.read_csv('hosp/diagnoses_icd.csv.gz')\n",
    "icu_pt = pd.read_csv('icu/icustays.csv.gz')\n",
    "df_lab = pd.read_csv('hosp/labevents.csv.gz')\n",
    "df_lab_id = pd.read_csv('hosp/d_labitems.csv.gz')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# ICU PT WITH > 24 H Data"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>subject_id</th>\n",
       "      <th>hadm_id</th>\n",
       "      <th>stay_id</th>\n",
       "      <th>first_careunit</th>\n",
       "      <th>last_careunit</th>\n",
       "      <th>intime</th>\n",
       "      <th>outtime</th>\n",
       "      <th>los</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>10000032</td>\n",
       "      <td>29079034</td>\n",
       "      <td>39553978</td>\n",
       "      <td>Medical Intensive Care Unit (MICU)</td>\n",
       "      <td>Medical Intensive Care Unit (MICU)</td>\n",
       "      <td>2180-07-23 14:00:00</td>\n",
       "      <td>2180-07-23 23:50:47</td>\n",
       "      <td>0.410266</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>10000980</td>\n",
       "      <td>26913865</td>\n",
       "      <td>39765666</td>\n",
       "      <td>Medical Intensive Care Unit (MICU)</td>\n",
       "      <td>Medical Intensive Care Unit (MICU)</td>\n",
       "      <td>2189-06-27 08:42:00</td>\n",
       "      <td>2189-06-27 20:38:27</td>\n",
       "      <td>0.497535</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>10001217</td>\n",
       "      <td>24597018</td>\n",
       "      <td>37067082</td>\n",
       "      <td>Surgical Intensive Care Unit (SICU)</td>\n",
       "      <td>Surgical Intensive Care Unit (SICU)</td>\n",
       "      <td>2157-11-20 19:18:02</td>\n",
       "      <td>2157-11-21 22:08:00</td>\n",
       "      <td>1.118032</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>10001217</td>\n",
       "      <td>27703517</td>\n",
       "      <td>34592300</td>\n",
       "      <td>Surgical Intensive Care Unit (SICU)</td>\n",
       "      <td>Surgical Intensive Care Unit (SICU)</td>\n",
       "      <td>2157-12-19 15:42:24</td>\n",
       "      <td>2157-12-20 14:27:41</td>\n",
       "      <td>0.948113</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>10001725</td>\n",
       "      <td>25563031</td>\n",
       "      <td>31205490</td>\n",
       "      <td>Medical/Surgical Intensive Care Unit (MICU/SICU)</td>\n",
       "      <td>Medical/Surgical Intensive Care Unit (MICU/SICU)</td>\n",
       "      <td>2110-04-11 15:52:22</td>\n",
       "      <td>2110-04-12 23:59:56</td>\n",
       "      <td>1.338588</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "   subject_id   hadm_id   stay_id  \\\n",
       "0    10000032  29079034  39553978   \n",
       "1    10000980  26913865  39765666   \n",
       "2    10001217  24597018  37067082   \n",
       "3    10001217  27703517  34592300   \n",
       "4    10001725  25563031  31205490   \n",
       "\n",
       "                                     first_careunit  \\\n",
       "0                Medical Intensive Care Unit (MICU)   \n",
       "1                Medical Intensive Care Unit (MICU)   \n",
       "2               Surgical Intensive Care Unit (SICU)   \n",
       "3               Surgical Intensive Care Unit (SICU)   \n",
       "4  Medical/Surgical Intensive Care Unit (MICU/SICU)   \n",
       "\n",
       "                                      last_careunit               intime  \\\n",
       "0                Medical Intensive Care Unit (MICU)  2180-07-23 14:00:00   \n",
       "1                Medical Intensive Care Unit (MICU)  2189-06-27 08:42:00   \n",
       "2               Surgical Intensive Care Unit (SICU)  2157-11-20 19:18:02   \n",
       "3               Surgical Intensive Care Unit (SICU)  2157-12-19 15:42:24   \n",
       "4  Medical/Surgical Intensive Care Unit (MICU/SICU)  2110-04-11 15:52:22   \n",
       "\n",
       "               outtime       los  \n",
       "0  2180-07-23 23:50:47  0.410266  \n",
       "1  2189-06-27 20:38:27  0.497535  \n",
       "2  2157-11-21 22:08:00  1.118032  \n",
       "3  2157-12-20 14:27:41  0.948113  \n",
       "4  2110-04-12 23:59:56  1.338588  "
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "icu_pt.head()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>subject_id</th>\n",
       "      <th>hadm_id</th>\n",
       "      <th>stay_id</th>\n",
       "      <th>first_careunit</th>\n",
       "      <th>last_careunit</th>\n",
       "      <th>intime</th>\n",
       "      <th>outtime</th>\n",
       "      <th>los</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>10001217</td>\n",
       "      <td>24597018</td>\n",
       "      <td>37067082</td>\n",
       "      <td>Surgical Intensive Care Unit (SICU)</td>\n",
       "      <td>Surgical Intensive Care Unit (SICU)</td>\n",
       "      <td>2157-11-20 19:18:02</td>\n",
       "      <td>2157-11-21 22:08:00</td>\n",
       "      <td>1.118032</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>10001725</td>\n",
       "      <td>25563031</td>\n",
       "      <td>31205490</td>\n",
       "      <td>Medical/Surgical Intensive Care Unit (MICU/SICU)</td>\n",
       "      <td>Medical/Surgical Intensive Care Unit (MICU/SICU)</td>\n",
       "      <td>2110-04-11 15:52:22</td>\n",
       "      <td>2110-04-12 23:59:56</td>\n",
       "      <td>1.338588</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>5</th>\n",
       "      <td>10001884</td>\n",
       "      <td>26184834</td>\n",
       "      <td>37510196</td>\n",
       "      <td>Medical Intensive Care Unit (MICU)</td>\n",
       "      <td>Medical Intensive Care Unit (MICU)</td>\n",
       "      <td>2131-01-11 04:20:05</td>\n",
       "      <td>2131-01-20 08:27:30</td>\n",
       "      <td>9.171817</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>6</th>\n",
       "      <td>10002013</td>\n",
       "      <td>23581541</td>\n",
       "      <td>39060235</td>\n",
       "      <td>Cardiac Vascular Intensive Care Unit (CVICU)</td>\n",
       "      <td>Cardiac Vascular Intensive Care Unit (CVICU)</td>\n",
       "      <td>2160-05-18 10:00:53</td>\n",
       "      <td>2160-05-19 17:33:33</td>\n",
       "      <td>1.314352</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>8</th>\n",
       "      <td>10002155</td>\n",
       "      <td>23822395</td>\n",
       "      <td>33685454</td>\n",
       "      <td>Coronary Care Unit (CCU)</td>\n",
       "      <td>Coronary Care Unit (CCU)</td>\n",
       "      <td>2129-08-04 12:45:00</td>\n",
       "      <td>2129-08-10 17:02:38</td>\n",
       "      <td>6.178912</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "   subject_id   hadm_id   stay_id  \\\n",
       "2    10001217  24597018  37067082   \n",
       "4    10001725  25563031  31205490   \n",
       "5    10001884  26184834  37510196   \n",
       "6    10002013  23581541  39060235   \n",
       "8    10002155  23822395  33685454   \n",
       "\n",
       "                                     first_careunit  \\\n",
       "2               Surgical Intensive Care Unit (SICU)   \n",
       "4  Medical/Surgical Intensive Care Unit (MICU/SICU)   \n",
       "5                Medical Intensive Care Unit (MICU)   \n",
       "6      Cardiac Vascular Intensive Care Unit (CVICU)   \n",
       "8                          Coronary Care Unit (CCU)   \n",
       "\n",
       "                                      last_careunit               intime  \\\n",
       "2               Surgical Intensive Care Unit (SICU)  2157-11-20 19:18:02   \n",
       "4  Medical/Surgical Intensive Care Unit (MICU/SICU)  2110-04-11 15:52:22   \n",
       "5                Medical Intensive Care Unit (MICU)  2131-01-11 04:20:05   \n",
       "6      Cardiac Vascular Intensive Care Unit (CVICU)  2160-05-18 10:00:53   \n",
       "8                          Coronary Care Unit (CCU)  2129-08-04 12:45:00   \n",
       "\n",
       "               outtime       los  \n",
       "2  2157-11-21 22:08:00  1.118032  \n",
       "4  2110-04-12 23:59:56  1.338588  \n",
       "5  2131-01-20 08:27:30  9.171817  \n",
       "6  2160-05-19 17:33:33  1.314352  \n",
       "8  2129-08-10 17:02:38  6.178912  "
      ]
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "icu_pt_24 = icu_pt[icu_pt['los']>=1]\n",
    "icu_pt_24.head()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {},
   "outputs": [],
   "source": [
    "patient_id = icu_pt_24['subject_id'].drop_duplicates()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "42264"
      ]
     },
     "execution_count": 11,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "len(patient_id)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "2      10001217\n",
       "4      10001725\n",
       "5      10001884\n",
       "6      10002013\n",
       "8      10002155\n",
       "         ...   \n",
       "149    10019917\n",
       "150    10019957\n",
       "152    10020187\n",
       "154    10020306\n",
       "155    10020640\n",
       "Name: subject_id, Length: 100, dtype: int64"
      ]
     },
     "execution_count": 12,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "patient_id[0:100]"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# HF ICU PT WITH >24H"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {},
   "outputs": [],
   "source": [
    "hfref_id = dx_icd[dx_icd['icd_code'].str.startswith('4282')|dx_icd['icd_code'].str.startswith('I502')].subject_id"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "metadata": {},
   "outputs": [],
   "source": [
    "hfpef_id = dx_icd[dx_icd['icd_code'].str.startswith('4283')|dx_icd['icd_code'].str.startswith('I503')].subject_id"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "metadata": {},
   "outputs": [],
   "source": [
    "hf_id = pd.concat([hfref_id, hfpef_id])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "metadata": {},
   "outputs": [],
   "source": [
    "hf_id = hf_id.drop_duplicates()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "20189"
      ]
     },
     "execution_count": 17,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "len(hf_id)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 27,
   "metadata": {},
   "outputs": [],
   "source": [
    "icu_hf_pt_24 = icu_pt_24[icu_pt_24[\"subject_id\"].isin(hf_id)]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 28,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "11110"
      ]
     },
     "execution_count": 28,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "len(icu_hf_pt_24[\"subject_id\"].unique())"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 105,
   "metadata": {},
   "outputs": [],
   "source": [
    "icu_hf_pt = icu_pt[icu_pt[\"subject_id\"].isin(hf_id)]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 106,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "12426"
      ]
     },
     "execution_count": 106,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "len(icu_hf_pt[\"subject_id\"].unique())"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 109,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "5971"
      ]
     },
     "execution_count": 109,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "sum(hfref_id.drop_duplicates().isin(icu_hf_pt_24[\"subject_id\"]))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 108,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "6436"
      ]
     },
     "execution_count": 108,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "sum(hfpef_id.drop_duplicates().isin(icu_hf_pt_24[\"subject_id\"]))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 78,
   "metadata": {},
   "outputs": [],
   "source": [
    "hfref_icd_24_id = icu_pt_24[icu_pt_24[\"subject_id\"].isin(hfref_id)][\"subject_id\"]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 79,
   "metadata": {},
   "outputs": [],
   "source": [
    "hfpef_icd_24_id = icu_pt_24[icu_pt_24[\"subject_id\"].isin(hfpef_id)][\"subject_id\"]"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# HF ICU PT WITH PROBNP VALUE AND >24H DATA"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 29,
   "metadata": {},
   "outputs": [],
   "source": [
    "df_lab_comp = pd.merge(df_lab, df_lab_id, on='itemid', how='left')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 30,
   "metadata": {},
   "outputs": [],
   "source": [
    "pt_with_pro_bnp = df_lab_comp[(df_lab_comp['itemid'] == 50963)]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 32,
   "metadata": {},
   "outputs": [],
   "source": [
    "# Using isin() to create a boolean mask where subject_id is present in pt_with_pro_bnp\n",
    "is_in_pro_bnp = icu_hf_pt_24[\"subject_id\"].isin(pt_with_pro_bnp[\"subject_id\"])\n",
    "\n",
    "# Using ~ to negate the boolean mask, giving us True where subject_id is NOT in pt_with_pro_bnp\n",
    "not_in_pro_bnp = ~is_in_pro_bnp\n",
    "\n",
    "# Selecting rows from icu_hf_pt where subject_id is NOT in pt_with_pro_bnp\n",
    "icu_hf_pt_24_without_bnp = icu_hf_pt_24[not_in_pro_bnp]\n",
    "\n",
    "icu_hf_pt_24_with_bnp = icu_hf_pt_24[is_in_pro_bnp]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 57,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>labevent_id</th>\n",
       "      <th>subject_id</th>\n",
       "      <th>hadm_id</th>\n",
       "      <th>specimen_id</th>\n",
       "      <th>itemid</th>\n",
       "      <th>order_provider_id</th>\n",
       "      <th>charttime</th>\n",
       "      <th>storetime</th>\n",
       "      <th>value</th>\n",
       "      <th>valuenum</th>\n",
       "      <th>valueuom</th>\n",
       "      <th>ref_range_lower</th>\n",
       "      <th>ref_range_upper</th>\n",
       "      <th>flag</th>\n",
       "      <th>priority</th>\n",
       "      <th>comments</th>\n",
       "      <th>label</th>\n",
       "      <th>fluid</th>\n",
       "      <th>category</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>5090</th>\n",
       "      <td>5099</td>\n",
       "      <td>10000935</td>\n",
       "      <td>25849114.0</td>\n",
       "      <td>55494709</td>\n",
       "      <td>50963</td>\n",
       "      <td>NaN</td>\n",
       "      <td>2187-10-11 06:25:00</td>\n",
       "      <td>2187-10-11 18:35:00</td>\n",
       "      <td>___</td>\n",
       "      <td>617.0</td>\n",
       "      <td>pg/mL</td>\n",
       "      <td>0.0</td>\n",
       "      <td>226.0</td>\n",
       "      <td>abnormal</td>\n",
       "      <td>ROUTINE</td>\n",
       "      <td>REFERENCE VALUES VARY WITH AGE, SEX, AND RENAL...</td>\n",
       "      <td>NTproBNP</td>\n",
       "      <td>Blood</td>\n",
       "      <td>Chemistry</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>5917</th>\n",
       "      <td>5927</td>\n",
       "      <td>10000980</td>\n",
       "      <td>NaN</td>\n",
       "      <td>33821244</td>\n",
       "      <td>50963</td>\n",
       "      <td>NaN</td>\n",
       "      <td>2188-01-03 13:18:00</td>\n",
       "      <td>2188-01-03 14:42:00</td>\n",
       "      <td>___</td>\n",
       "      <td>4571.0</td>\n",
       "      <td>pg/mL</td>\n",
       "      <td>0.0</td>\n",
       "      <td>624.0</td>\n",
       "      <td>abnormal</td>\n",
       "      <td>STAT</td>\n",
       "      <td>REFERENCE VALUES VARY WITH AGE, SEX, AND RENAL...</td>\n",
       "      <td>NTproBNP</td>\n",
       "      <td>Blood</td>\n",
       "      <td>Chemistry</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "      labevent_id  subject_id     hadm_id  specimen_id  itemid  \\\n",
       "5090         5099    10000935  25849114.0     55494709   50963   \n",
       "5917         5927    10000980         NaN     33821244   50963   \n",
       "\n",
       "     order_provider_id            charttime            storetime value  \\\n",
       "5090               NaN  2187-10-11 06:25:00  2187-10-11 18:35:00   ___   \n",
       "5917               NaN  2188-01-03 13:18:00  2188-01-03 14:42:00   ___   \n",
       "\n",
       "      valuenum valueuom  ref_range_lower  ref_range_upper      flag priority  \\\n",
       "5090     617.0    pg/mL              0.0            226.0  abnormal  ROUTINE   \n",
       "5917    4571.0    pg/mL              0.0            624.0  abnormal     STAT   \n",
       "\n",
       "                                               comments     label  fluid  \\\n",
       "5090  REFERENCE VALUES VARY WITH AGE, SEX, AND RENAL...  NTproBNP  Blood   \n",
       "5917  REFERENCE VALUES VARY WITH AGE, SEX, AND RENAL...  NTproBNP  Blood   \n",
       "\n",
       "       category  \n",
       "5090  Chemistry  \n",
       "5917  Chemistry  "
      ]
     },
     "execution_count": 57,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "pt_with_pro_bnp.head(2)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 61,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>labevent_id</th>\n",
       "      <th>subject_id</th>\n",
       "      <th>hadm_id</th>\n",
       "      <th>specimen_id</th>\n",
       "      <th>itemid</th>\n",
       "      <th>order_provider_id</th>\n",
       "      <th>charttime</th>\n",
       "      <th>storetime</th>\n",
       "      <th>value</th>\n",
       "      <th>valuenum</th>\n",
       "      <th>valueuom</th>\n",
       "      <th>ref_range_lower</th>\n",
       "      <th>ref_range_upper</th>\n",
       "      <th>flag</th>\n",
       "      <th>priority</th>\n",
       "      <th>comments</th>\n",
       "      <th>label</th>\n",
       "      <th>fluid</th>\n",
       "      <th>category</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>5917</th>\n",
       "      <td>5927</td>\n",
       "      <td>10000980</td>\n",
       "      <td>NaN</td>\n",
       "      <td>33821244</td>\n",
       "      <td>50963</td>\n",
       "      <td>NaN</td>\n",
       "      <td>2188-01-03 13:18:00</td>\n",
       "      <td>2188-01-03 14:42:00</td>\n",
       "      <td>___</td>\n",
       "      <td>4571.0</td>\n",
       "      <td>pg/mL</td>\n",
       "      <td>0.0</td>\n",
       "      <td>624.0</td>\n",
       "      <td>abnormal</td>\n",
       "      <td>STAT</td>\n",
       "      <td>REFERENCE VALUES VARY WITH AGE, SEX, AND RENAL...</td>\n",
       "      <td>NTproBNP</td>\n",
       "      <td>Blood</td>\n",
       "      <td>Chemistry</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>6206</th>\n",
       "      <td>6216</td>\n",
       "      <td>10000980</td>\n",
       "      <td>NaN</td>\n",
       "      <td>46753637</td>\n",
       "      <td>50963</td>\n",
       "      <td>NaN</td>\n",
       "      <td>2189-06-27 06:48:00</td>\n",
       "      <td>2189-06-27 07:56:00</td>\n",
       "      <td>___</td>\n",
       "      <td>2826.0</td>\n",
       "      <td>pg/mL</td>\n",
       "      <td>0.0</td>\n",
       "      <td>624.0</td>\n",
       "      <td>abnormal</td>\n",
       "      <td>STAT</td>\n",
       "      <td>REFERENCE VALUES VARY WITH AGE, SEX, AND RENAL...</td>\n",
       "      <td>NTproBNP</td>\n",
       "      <td>Blood</td>\n",
       "      <td>Chemistry</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>6891</th>\n",
       "      <td>6902</td>\n",
       "      <td>10000980</td>\n",
       "      <td>NaN</td>\n",
       "      <td>88477575</td>\n",
       "      <td>50963</td>\n",
       "      <td>NaN</td>\n",
       "      <td>2190-11-06 16:00:00</td>\n",
       "      <td>2190-11-06 18:12:00</td>\n",
       "      <td>___</td>\n",
       "      <td>4279.0</td>\n",
       "      <td>pg/mL</td>\n",
       "      <td>0.0</td>\n",
       "      <td>624.0</td>\n",
       "      <td>abnormal</td>\n",
       "      <td>STAT</td>\n",
       "      <td>REFERENCE VALUES VARY WITH AGE, SEX, AND RENAL...</td>\n",
       "      <td>NTproBNP</td>\n",
       "      <td>Blood</td>\n",
       "      <td>Chemistry</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>7582</th>\n",
       "      <td>7593</td>\n",
       "      <td>10000980</td>\n",
       "      <td>NaN</td>\n",
       "      <td>77976228</td>\n",
       "      <td>50963</td>\n",
       "      <td>NaN</td>\n",
       "      <td>2191-05-30 12:40:00</td>\n",
       "      <td>2191-05-30 13:55:00</td>\n",
       "      <td>___</td>\n",
       "      <td>6440.0</td>\n",
       "      <td>pg/mL</td>\n",
       "      <td>0.0</td>\n",
       "      <td>624.0</td>\n",
       "      <td>abnormal</td>\n",
       "      <td>STAT</td>\n",
       "      <td>REFERENCE VALUES VARY WITH AGE, SEX, AND RENAL...</td>\n",
       "      <td>NTproBNP</td>\n",
       "      <td>Blood</td>\n",
       "      <td>Chemistry</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>7725</th>\n",
       "      <td>7736</td>\n",
       "      <td>10000980</td>\n",
       "      <td>NaN</td>\n",
       "      <td>52441038</td>\n",
       "      <td>50963</td>\n",
       "      <td>NaN</td>\n",
       "      <td>2191-07-16 11:55:00</td>\n",
       "      <td>2191-07-16 13:37:00</td>\n",
       "      <td>___</td>\n",
       "      <td>10180.0</td>\n",
       "      <td>pg/mL</td>\n",
       "      <td>0.0</td>\n",
       "      <td>624.0</td>\n",
       "      <td>abnormal</td>\n",
       "      <td>STAT</td>\n",
       "      <td>REFERENCE VALUES VARY WITH AGE, SEX, AND RENAL...</td>\n",
       "      <td>NTproBNP</td>\n",
       "      <td>Blood</td>\n",
       "      <td>Chemistry</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "      labevent_id  subject_id  hadm_id  specimen_id  itemid order_provider_id  \\\n",
       "5917         5927    10000980      NaN     33821244   50963               NaN   \n",
       "6206         6216    10000980      NaN     46753637   50963               NaN   \n",
       "6891         6902    10000980      NaN     88477575   50963               NaN   \n",
       "7582         7593    10000980      NaN     77976228   50963               NaN   \n",
       "7725         7736    10000980      NaN     52441038   50963               NaN   \n",
       "\n",
       "                charttime            storetime value  valuenum valueuom  \\\n",
       "5917  2188-01-03 13:18:00  2188-01-03 14:42:00   ___    4571.0    pg/mL   \n",
       "6206  2189-06-27 06:48:00  2189-06-27 07:56:00   ___    2826.0    pg/mL   \n",
       "6891  2190-11-06 16:00:00  2190-11-06 18:12:00   ___    4279.0    pg/mL   \n",
       "7582  2191-05-30 12:40:00  2191-05-30 13:55:00   ___    6440.0    pg/mL   \n",
       "7725  2191-07-16 11:55:00  2191-07-16 13:37:00   ___   10180.0    pg/mL   \n",
       "\n",
       "      ref_range_lower  ref_range_upper      flag priority  \\\n",
       "5917              0.0            624.0  abnormal     STAT   \n",
       "6206              0.0            624.0  abnormal     STAT   \n",
       "6891              0.0            624.0  abnormal     STAT   \n",
       "7582              0.0            624.0  abnormal     STAT   \n",
       "7725              0.0            624.0  abnormal     STAT   \n",
       "\n",
       "                                               comments     label  fluid  \\\n",
       "5917  REFERENCE VALUES VARY WITH AGE, SEX, AND RENAL...  NTproBNP  Blood   \n",
       "6206  REFERENCE VALUES VARY WITH AGE, SEX, AND RENAL...  NTproBNP  Blood   \n",
       "6891  REFERENCE VALUES VARY WITH AGE, SEX, AND RENAL...  NTproBNP  Blood   \n",
       "7582  REFERENCE VALUES VARY WITH AGE, SEX, AND RENAL...  NTproBNP  Blood   \n",
       "7725  REFERENCE VALUES VARY WITH AGE, SEX, AND RENAL...  NTproBNP  Blood   \n",
       "\n",
       "       category  \n",
       "5917  Chemistry  \n",
       "6206  Chemistry  \n",
       "6891  Chemistry  \n",
       "7582  Chemistry  \n",
       "7725  Chemistry  "
      ]
     },
     "execution_count": 61,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "pt_with_pro_bnp[(pt_with_pro_bnp[\"subject_id\"].isin(hfref_id)) & (pt_with_pro_bnp[\"valuenum\"] > 200)].head()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Number of ICU HFrEF pt with >24h data WITH PROBNP "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 65,
   "metadata": {},
   "outputs": [],
   "source": [
    "positive_pro_bnp = pt_with_pro_bnp[pt_with_pro_bnp[\"valuenum\"] > 200]\n",
    "negative_pro_bnp = pt_with_pro_bnp[pt_with_pro_bnp[\"valuenum\"] <= 200]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 80,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "3721"
      ]
     },
     "execution_count": 80,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "len(positive_pro_bnp[positive_pro_bnp[\"subject_id\"].isin(hfref_icd_24_id)][\"subject_id\"].drop_duplicates())"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 81,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "291"
      ]
     },
     "execution_count": 81,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "len(negative_pro_bnp[negative_pro_bnp[\"subject_id\"].isin(hfref_icd_24_id)][\"subject_id\"].drop_duplicates())"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Number of ICU HFpEF pt with >24h data WITH PROBNP "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 82,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "4455"
      ]
     },
     "execution_count": 82,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "len(positive_pro_bnp[positive_pro_bnp[\"subject_id\"].isin(hfpef_icd_24_id)][\"subject_id\"].drop_duplicates())"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 83,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "566"
      ]
     },
     "execution_count": 83,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "len(negative_pro_bnp[negative_pro_bnp[\"subject_id\"].isin(hfpef_icd_24_id)][\"subject_id\"].drop_duplicates())"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Number of ICU pt with >24h data WITH PROBNP and CONFLICTING DX"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 86,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "1110"
      ]
     },
     "execution_count": 86,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "len(positive_pro_bnp[(positive_pro_bnp[\"subject_id\"].isin(hfref_icd_24_id)) & (positive_pro_bnp[\"subject_id\"].isin\\\n",
    "                                                                               (hfpef_icd_24_id))][\"subject_id\"].drop_duplicates())"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 87,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "123"
      ]
     },
     "execution_count": 87,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "len(negative_pro_bnp[(negative_pro_bnp[\"subject_id\"].isin(hfref_icd_24_id)) & (negative_pro_bnp[\"subject_id\"].isin\\\n",
    "                                                                               (hfpef_icd_24_id))][\"subject_id\"].drop_duplicates())"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Number of ICU HFrEF pt with >24h data WITH PROBNP > 200"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 62,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "6106"
      ]
     },
     "execution_count": 62,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "len(pt_with_pro_bnp[(pt_with_pro_bnp[\"subject_id\"].isin(hfref_id)) & (pt_with_pro_bnp[\"valuenum\"] > 200)]\\\n",
    "    [\"subject_id\"].drop_duplicates())"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Number of ICU HFrEF pt with >24h data WITH PROBNP <= 200"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 73,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "541"
      ]
     },
     "execution_count": 73,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "len(pt_with_pro_bnp[(pt_with_pro_bnp[\"subject_id\"].isin(hfref_id)) & (pt_with_pro_bnp[\"valuenum\"] <= 200)]\\\n",
    "    [\"subject_id\"].drop_duplicates())"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Number of ICU HFpEF pt with >24h data WITH PROBNP > 200"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 47,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "4644"
      ]
     },
     "execution_count": 47,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "len(pt_with_pro_bnp[(pt_with_pro_bnp[\"subject_id\"].isin(hfpef_id)) & (pt_with_pro_bnp[\"valuenum\"] > 200)]\\\n",
    "    [\"subject_id\"].drop_duplicates())"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Number of ICU HFpEF pt with >24h data WITH PROBNP <= 200"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 74,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "1092"
      ]
     },
     "execution_count": 74,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "len(pt_with_pro_bnp[(pt_with_pro_bnp[\"subject_id\"].isin(hfpef_id)) & (pt_with_pro_bnp[\"valuenum\"] <= 200)]\\\n",
    "    [\"subject_id\"].drop_duplicates())"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Number of ICU HF pt with >24h data WITH PROBNP and CONFLICTING ICD DX"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 52,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "1136"
      ]
     },
     "execution_count": 52,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "len(icu_hf_pt_24_with_bnp[icu_hf_pt_24_with_bnp[\"subject_id\"].isin(hfpef_id)&icu_hf_pt_24_with_bnp[\"subject_id\"]\\\n",
    "                          .isin(hfref_id)].subject_id.unique())"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 51,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "7373"
      ]
     },
     "execution_count": 51,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "3865 + 4644 - 1136"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Number of ICU HF pt with >24h data WITHOUT PROBNP"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 33,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "3737"
      ]
     },
     "execution_count": 33,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "len(icu_hf_pt_24_without_bnp[\"subject_id\"].drop_duplicates())"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Number of ICU HFrEF pt with >24h data without PROBNP"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 110,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "2106"
      ]
     },
     "execution_count": 110,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "sum(icu_hf_pt_24_without_bnp[\"subject_id\"].drop_duplicates().isin(hfref_icd_24_id))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Number of ICU HFpEF pt with >24h data without PROBNP"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 111,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "1792"
      ]
     },
     "execution_count": 111,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "sum(icu_hf_pt_24_without_bnp[\"subject_id\"].drop_duplicates().isin(hfpef_icd_24_id))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Number of ICU pt with >24h data without PROBNP and  CONFLICTING ICD DX"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 41,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "161"
      ]
     },
     "execution_count": 41,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "len(icu_hf_pt_24_without_bnp[icu_hf_pt_24_without_bnp[\"subject_id\"].isin(hfpef_id)&icu_hf_pt_24_without_bnp[\"subject_id\"]\\\n",
    "                             .isin(hfref_id)].subject_id.unique())"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 42,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "3737"
      ]
     },
     "execution_count": 42,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "2106+1792-161"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# ICU with 24h and absence of HF ICD codes with ProBNP"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 88,
   "metadata": {},
   "outputs": [],
   "source": [
    "icu_non_hf_pt_24 = icu_pt_24[~(icu_pt_24[\"subject_id\"].isin(hf_id))]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 94,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>subject_id</th>\n",
       "      <th>hadm_id</th>\n",
       "      <th>stay_id</th>\n",
       "      <th>first_careunit</th>\n",
       "      <th>last_careunit</th>\n",
       "      <th>intime</th>\n",
       "      <th>outtime</th>\n",
       "      <th>los</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>10001217</td>\n",
       "      <td>24597018</td>\n",
       "      <td>37067082</td>\n",
       "      <td>Surgical Intensive Care Unit (SICU)</td>\n",
       "      <td>Surgical Intensive Care Unit (SICU)</td>\n",
       "      <td>2157-11-20 19:18:02</td>\n",
       "      <td>2157-11-21 22:08:00</td>\n",
       "      <td>1.118032</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>10001725</td>\n",
       "      <td>25563031</td>\n",
       "      <td>31205490</td>\n",
       "      <td>Medical/Surgical Intensive Care Unit (MICU/SICU)</td>\n",
       "      <td>Medical/Surgical Intensive Care Unit (MICU/SICU)</td>\n",
       "      <td>2110-04-11 15:52:22</td>\n",
       "      <td>2110-04-12 23:59:56</td>\n",
       "      <td>1.338588</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>5</th>\n",
       "      <td>10001884</td>\n",
       "      <td>26184834</td>\n",
       "      <td>37510196</td>\n",
       "      <td>Medical Intensive Care Unit (MICU)</td>\n",
       "      <td>Medical Intensive Care Unit (MICU)</td>\n",
       "      <td>2131-01-11 04:20:05</td>\n",
       "      <td>2131-01-20 08:27:30</td>\n",
       "      <td>9.171817</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>10</th>\n",
       "      <td>10002348</td>\n",
       "      <td>22725460</td>\n",
       "      <td>32610785</td>\n",
       "      <td>Neuro Intermediate</td>\n",
       "      <td>Neuro Intermediate</td>\n",
       "      <td>2112-11-30 23:24:00</td>\n",
       "      <td>2112-12-10 18:25:13</td>\n",
       "      <td>9.792512</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>11</th>\n",
       "      <td>10002428</td>\n",
       "      <td>20321825</td>\n",
       "      <td>34807493</td>\n",
       "      <td>Medical Intensive Care Unit (MICU)</td>\n",
       "      <td>Medical Intensive Care Unit (MICU)</td>\n",
       "      <td>2156-04-30 21:53:00</td>\n",
       "      <td>2156-05-02 22:27:20</td>\n",
       "      <td>2.023843</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "    subject_id   hadm_id   stay_id  \\\n",
       "2     10001217  24597018  37067082   \n",
       "4     10001725  25563031  31205490   \n",
       "5     10001884  26184834  37510196   \n",
       "10    10002348  22725460  32610785   \n",
       "11    10002428  20321825  34807493   \n",
       "\n",
       "                                      first_careunit  \\\n",
       "2                Surgical Intensive Care Unit (SICU)   \n",
       "4   Medical/Surgical Intensive Care Unit (MICU/SICU)   \n",
       "5                 Medical Intensive Care Unit (MICU)   \n",
       "10                                Neuro Intermediate   \n",
       "11                Medical Intensive Care Unit (MICU)   \n",
       "\n",
       "                                       last_careunit               intime  \\\n",
       "2                Surgical Intensive Care Unit (SICU)  2157-11-20 19:18:02   \n",
       "4   Medical/Surgical Intensive Care Unit (MICU/SICU)  2110-04-11 15:52:22   \n",
       "5                 Medical Intensive Care Unit (MICU)  2131-01-11 04:20:05   \n",
       "10                                Neuro Intermediate  2112-11-30 23:24:00   \n",
       "11                Medical Intensive Care Unit (MICU)  2156-04-30 21:53:00   \n",
       "\n",
       "                outtime       los  \n",
       "2   2157-11-21 22:08:00  1.118032  \n",
       "4   2110-04-12 23:59:56  1.338588  \n",
       "5   2131-01-20 08:27:30  9.171817  \n",
       "10  2112-12-10 18:25:13  9.792512  \n",
       "11  2156-05-02 22:27:20  2.023843  "
      ]
     },
     "execution_count": 94,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "icu_non_hf_pt_24.head()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 93,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>subject_id</th>\n",
       "      <th>hadm_id</th>\n",
       "      <th>seq_num</th>\n",
       "      <th>icd_code</th>\n",
       "      <th>icd_version</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>409</th>\n",
       "      <td>10001217</td>\n",
       "      <td>24597018</td>\n",
       "      <td>1</td>\n",
       "      <td>3240</td>\n",
       "      <td>9</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>410</th>\n",
       "      <td>10001217</td>\n",
       "      <td>24597018</td>\n",
       "      <td>2</td>\n",
       "      <td>3484</td>\n",
       "      <td>9</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>411</th>\n",
       "      <td>10001217</td>\n",
       "      <td>24597018</td>\n",
       "      <td>3</td>\n",
       "      <td>3485</td>\n",
       "      <td>9</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>412</th>\n",
       "      <td>10001217</td>\n",
       "      <td>24597018</td>\n",
       "      <td>4</td>\n",
       "      <td>5180</td>\n",
       "      <td>9</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>413</th>\n",
       "      <td>10001217</td>\n",
       "      <td>24597018</td>\n",
       "      <td>5</td>\n",
       "      <td>340</td>\n",
       "      <td>9</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "     subject_id   hadm_id  seq_num icd_code  icd_version\n",
       "409    10001217  24597018        1     3240            9\n",
       "410    10001217  24597018        2     3484            9\n",
       "411    10001217  24597018        3     3485            9\n",
       "412    10001217  24597018        4     5180            9\n",
       "413    10001217  24597018        5      340            9"
      ]
     },
     "execution_count": 93,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "dx_icd[dx_icd[\"subject_id\"].isin(icu_non_hf_pt_24[\"subject_id\"])].head()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 98,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>labevent_id</th>\n",
       "      <th>subject_id</th>\n",
       "      <th>hadm_id</th>\n",
       "      <th>specimen_id</th>\n",
       "      <th>itemid</th>\n",
       "      <th>order_provider_id</th>\n",
       "      <th>charttime</th>\n",
       "      <th>storetime</th>\n",
       "      <th>value</th>\n",
       "      <th>valuenum</th>\n",
       "      <th>valueuom</th>\n",
       "      <th>ref_range_lower</th>\n",
       "      <th>ref_range_upper</th>\n",
       "      <th>flag</th>\n",
       "      <th>priority</th>\n",
       "      <th>comments</th>\n",
       "      <th>label</th>\n",
       "      <th>fluid</th>\n",
       "      <th>category</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>12842</th>\n",
       "      <td>12858</td>\n",
       "      <td>10001725</td>\n",
       "      <td>NaN</td>\n",
       "      <td>58052149</td>\n",
       "      <td>50963</td>\n",
       "      <td>P76BRY</td>\n",
       "      <td>2111-05-16 12:40:00</td>\n",
       "      <td>2111-05-16 21:56:00</td>\n",
       "      <td>___</td>\n",
       "      <td>35.0</td>\n",
       "      <td>pg/mL</td>\n",
       "      <td>0.0</td>\n",
       "      <td>192.0</td>\n",
       "      <td>NaN</td>\n",
       "      <td>ROUTINE</td>\n",
       "      <td>REFERENCE VALUES VARY WITH AGE, SEX, AND RENAL...</td>\n",
       "      <td>NTproBNP</td>\n",
       "      <td>Blood</td>\n",
       "      <td>Chemistry</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>14858</th>\n",
       "      <td>14876</td>\n",
       "      <td>10001884</td>\n",
       "      <td>NaN</td>\n",
       "      <td>4029327</td>\n",
       "      <td>50963</td>\n",
       "      <td>P46WR5</td>\n",
       "      <td>2128-07-15 12:39:00</td>\n",
       "      <td>2128-07-15 20:19:00</td>\n",
       "      <td>___</td>\n",
       "      <td>233.0</td>\n",
       "      <td>pg/mL</td>\n",
       "      <td>0.0</td>\n",
       "      <td>353.0</td>\n",
       "      <td>NaN</td>\n",
       "      <td>ROUTINE</td>\n",
       "      <td>REFERENCE VALUES VARY WITH AGE, SEX, AND RENAL...</td>\n",
       "      <td>NTproBNP</td>\n",
       "      <td>Blood</td>\n",
       "      <td>Chemistry</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "       labevent_id  subject_id  hadm_id  specimen_id  itemid  \\\n",
       "12842        12858    10001725      NaN     58052149   50963   \n",
       "14858        14876    10001884      NaN      4029327   50963   \n",
       "\n",
       "      order_provider_id            charttime            storetime value  \\\n",
       "12842            P76BRY  2111-05-16 12:40:00  2111-05-16 21:56:00   ___   \n",
       "14858            P46WR5  2128-07-15 12:39:00  2128-07-15 20:19:00   ___   \n",
       "\n",
       "       valuenum valueuom  ref_range_lower  ref_range_upper flag priority  \\\n",
       "12842      35.0    pg/mL              0.0            192.0  NaN  ROUTINE   \n",
       "14858     233.0    pg/mL              0.0            353.0  NaN  ROUTINE   \n",
       "\n",
       "                                                comments     label  fluid  \\\n",
       "12842  REFERENCE VALUES VARY WITH AGE, SEX, AND RENAL...  NTproBNP  Blood   \n",
       "14858  REFERENCE VALUES VARY WITH AGE, SEX, AND RENAL...  NTproBNP  Blood   \n",
       "\n",
       "        category  \n",
       "12842  Chemistry  \n",
       "14858  Chemistry  "
      ]
     },
     "execution_count": 98,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "pt_with_pro_bnp[pt_with_pro_bnp[\"subject_id\"].isin(icu_non_hf_pt_24[\"subject_id\"])].head(2)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 101,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "8507"
      ]
     },
     "execution_count": 101,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "len(pt_with_pro_bnp[pt_with_pro_bnp[\"subject_id\"].isin(icu_non_hf_pt_24[\"subject_id\"])].drop_duplicates())"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 112,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "8337"
      ]
     },
     "execution_count": 112,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "len(icu_non_hf_pt_24[icu_non_hf_pt_24[\"subject_id\"].isin(pt_with_pro_bnp[\"subject_id\"])].drop_duplicates())"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 113,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "6516"
      ]
     },
     "execution_count": 113,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "len(icu_non_hf_pt_24[icu_non_hf_pt_24[\"subject_id\"].isin(positive_pro_bnp[\"subject_id\"])].drop_duplicates())"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 114,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "2213"
      ]
     },
     "execution_count": 114,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "len(icu_non_hf_pt_24[icu_non_hf_pt_24[\"subject_id\"].isin(negative_pro_bnp[\"subject_id\"])].drop_duplicates())"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# ICU with 24h and absence of HF ICD codes and without ProBNP"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 103,
   "metadata": {},
   "outputs": [],
   "source": [
    "icu_24_non_hf_non_bnp = icu_non_hf_pt_24[~icu_non_hf_pt_24[\"subject_id\"].isin(pt_with_pro_bnp[\"subject_id\"])]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 115,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "25778"
      ]
     },
     "execution_count": 115,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "len(icu_24_non_hf_non_bnp[\"subject_id\"].drop_duplicates())"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.5"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
